package com.anysoft.controller;

import com.anysoft.model.LoginUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/userLoginPrac")
public class LoginPracController {
    /**
     * 跳登录页
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 判断登录
     * @param user
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(LoginUser user, Model model, HttpSession session){
        System.out.println(user.getUname());
        String name = "zhangsan";
        String pwd = "123456";
        if(name.equals(user.getUname())&&pwd.equals(user.getUpwd())){
            session.setAttribute("user",user);
            return "redirect:main";
        }
        model.addAttribute("msg", "用户名或密码错误，请重新登录！ ");
        return "login";
    }

    /**
     * 跳转主页面
     * @return
     */
    @RequestMapping("/main")
    public String main(){
        return "main";
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        // 清除session
        session.invalidate();
        return "login";
    }
}
